iT邦幫忙

0

Javascript 指令詳解

  • 分享至 

  • xImage
  •  

取得字串的 bytes

/**
 * 計算字串的 byte 數
 *
 * @return 回傳 byte 數
 */
String.prototype.getStringBytes = function() {
	return this.replace(/[^\x00-\xff]/g,"xx").length;
};

/**
 * 計算字串的 byte 數
 *
 * @return 回傳 byte 數
 */
String.prototype.getStringChars = function() {
    var cnt = 0;
    for (var i = 0; i < this.length; i++) {
        var c = this.charAt(i);
        if (/^[\u0000-\u00ff]$/.test(c)) {
            cnt++;
        } else {
            cnt += 2;
        }
    }
    return cnt;
};

如何操作 select 和 option

JS 碼如下:

var yesDateYear = "2023";
var yesDateMonth = "12";
var yesDateDay = "31";

var iDateYear = window.document.getElementById("iDateYear");
var iDateMonth = window.document.getElementById("iDateMonth");
var iDateDay = window.document.getElementById("iDateDay");

for (var i = 0; i < iDateYear.length; i++) {
    if (iDateYear.options[i].value == yesDateYear) {
        iDateYear.options[i].selected = true;
    }
}
for (var j = 0; j < iDateMonth.length; j++) {
    if (iDateMonth.options[j].value == yesDateMonth) {
        iDateMonth.options[j].selected = true;
    }
}
for (var k = 0; k < iDateDay.length; k++) {
    if (iDateDay.options[k].value == yesDateDay) {
        iDateDay.options[k].selected = true;
    }
}

HTML 碼如下:

<select name="dateYear" id="iDateYear"></select>
<select name="dateMonth" id="iDateMonth"></select>
<select name="dateDate" id="iDateDay"></select>


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言